//------------------------------------------------------------------------------------------------------------

function toBack() {
	self.location = contextPath + "salesorder/page";
}

function doadd() {
	var newlength = Number($("#length").val()) + 1;
	$("#length").val(newlength);

	var text1 = $("<tr></tr>").attr("id", "radiotable_tr_" + newlength);

	var text2 = $("<td align='center' style='word-break: break-all;' ></td>");
	var text3 = $("<input >").attr({
		"name" : "orderDetails[" + newlength + "].productName",
		"class" : "productName",
		"id" : "productName_" + newlength,
		"tag" : "0",
		"onclick" : "show_childPage_product('" + newlength + "')"

	});

	var text18 = $("<input >").attr({
		"name" : "orderDetails[" + newlength + "].productId",
		"class" : "productId",
		"id" : "productId" + newlength,
		"type" : "hidden"

	});
	var text4 = $("<td align='center' style='word-break: break-all;' ></td>");
	var text5 = $("<input >").attr({
		"name" : "orderDetails[" + newlength + "].productPrice",
		"class" : "productPrice",
		"style" : "width: 100px; text-align: center;",
		"id" : "productPrice_" + newlength,
		"value" : "0"
	});

	var text6 = $("<td align='center' style='word-break: break-all;' ></td>");
	var text7 = $("<input >").attr({
		"name" : "orderDetails[" + newlength + "].productNum",
		"class" : "productNum",
		"style" : "width: 100px; text-align: center;",
		"id" : "productNum_" + newlength,
		"value" : "1"
	});

	var text8 = $("<td align='center' style='word-break: break-all;' ></td>");
	var text9 = $("<select></select>").attr({
		"name" : "orderDetails[" + newlength + "].discount",
		"class" : "discount",
		"style" : "width: 100px; text-align: center;",
		"id" : "discount_" + newlength
	});
	var text17 = $("<option>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp无折扣</option>").attr({
		"value" : "1",
		"selected" : "selected",
		"style" : "text-align: center;"
	})
	var text14 = $("<option>0.8</option>").attr({
		"value" : "0.8",

	})
	var text15 = $("<option>0.9</option>").attr({
		"value" : "0.9"
	})
	var text16 = $("<option>0.95</option>").attr({
		"value" : "0.95"
	})

	var text10 = $("<td align='center' style='word-break: break-all;' ></td>");
	var text11 = $("<input >").attr({
		"name" : "orderDetails[" + newlength + "].productCost",
		"class" : "productCost",
		"id" : "productCost_" + newlength,
		"value" : "0",
		"style" : " text-align: center;"
	});

	var text12 = $("<td align='center' style='word-break: break-all;' ></td>");
	var text13 = $("<input >").attr({
		"type" : "button",
		"onclick" : "deleteContact( 'radiotable_tr_" + newlength + "')",
		"value" : " delete ",
		"class" : "button"
	});

	text2.append(text3, text18);
	text4.append(text5);
	text6.append(text7);
	text9.append(text17, text14, text15, text16);
	text8.append(text9);
	text10.append(text11);
	text12.append(text13);

	text1.append(text2, text4, text6, text8, text10, text12);
	// .find(".productPrice").blur(count).end()
	$("#radiotable_1").append(text1);

}

function deleteContact(id) {

	$("#" + id).remove();
}

function show_child_employeeInfo() {
	window.open(contextPath + "employeeinfo/childpage", "child",
			"height=500,width=1000,top=50,left=50");
}

function show_child_customer() {
	window.open(contextPath + "customer/childpage", "child",
			"height=500,width=1000,top=50,left=50");
}

function callBack(id, name, grender, attachmentId) {

	$("#employeeInfoId").val(id);
	$("#employeeInfoName").val(name);

}

function count() {
	var discount = $(".discount");
	var productPrice = $(".productPrice");
	var productNum = $(".productNum");
	var productCost = $(".productCost")
	var sum = 0;
	for (i = 0; i < discount.length; i++) {
		var cost = $(".productPrice:eq(" + i + ")").val()
				* $(".productNum:eq(" + i + ")").val()
				* $(".discount:eq(" + i + ")").val()
		$(".productCost:eq(" + i + ")").val(cost.toFixed(3));

	}
	for (i = 0; i < productCost.length; i++) {
		sum += Number($(".productCost:eq(" + i + ")").val());

	}

	$("#sumCost").val(sum.toFixed(3));
}

function callBackProduct(productname, productid, productPrice) {

	var productId = $(".productId");
	var productName = $(".productName");

	for (i = 0; i < productName.length; i++) {

		if ($(".productName:eq(" + i + ")").attr("tag") == 1) {

			$(".productName:eq(" + i + ")").val(productname);
			$(".productId:eq(" + i + ")").val(productid);
			$(".productPrice:eq(" + i + ")").val(productPrice);
			$(".productName:eq(" + i + ")").attr("tag", "0");

		}
	}
}

function show_childPage_product(index) {

	$(".productName:eq(" + index + ")").attr("tag", "1");
	window.open(contextPath + "product/childpage", "child",
			"height=500,width=1000,top=50,left=50");
}

$(".productPrice").live("blur", function() {
	count();
});

$(".discount").live("blur", function() {
	count();
});
$(".productNum").live("blur", function() {
	count();
});

$(".productName").live("blur", function() {
	count();
});

document.onkeydown = keyEnter

function keyEnter(e) {
	var iKeyCode = window.event.keyCode;
	if (iKeyCode == 13)
		return false;
}

function deleteDemo(id) {
	if (confirm("你确定删除此记录吗?")) {
		$('#id').val(id);
		deleteForm.action = contextPath +"salesorder/"+ id;
		deleteForm.submit();
	}
}

function goPage(pageIndex) {
	$('#pageIndex').val(pageIndex);
	toPage.action =contextPath + "salesorder/page";

	toPage.submit();
}

function search() {

	var costBegin = $('#costBegin').val().split("");
	var costEnd = $('#costEnd').val().split("");

	if (costBegin.length <= 0) {
		$('#costBegin').val('0');
	}
	if (costEnd.length <= 0) {
		$('#costEnd').val('0');
	}

	numCheckBegin(costBegin);
	numCheckEnd(costEnd);

	searchForm.action = contextPath + "salesorder/page";
	searchForm.submit();
}

function skip() {

	var skip = $("#skip").val();
	var skipPageIndex = $('#skipPageIndex').val();
	var totalPage = $('#totalPage').val();

	if (skip.split("").length < 1) {

		goPage(skipPageIndex);
	} else if (skip.split("").length > totalPage.split("").length) {

		totalPage = totalPage - 1
		goPage(totalPage);

	} else if (skip > totalPage) {

		totalPage = totalPage - 1
		goPage(totalPage);

	}
	if (skip.split("").length > 0
			& skip.split("").length <= totalPage.split("").length) {
		skip = skip - 1

		goPage(skip);
	}
}

var num = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' ];

function numCheckBegin(obj) {
	$.each(obj, function(key, value) {
		if ($.inArray(value, num) < 0) {// 存在非数字时，结果为假

			$('#costBegin').val('0');
			return false;
		}
	})
}

function numCheckEnd(obj) {
	$.each(obj, function(key, value) {
		if ($.inArray(value, num) < 0) {// 存在非数字时，结果为假

			$('#costEnd').val('0');
			return false;
		}
	})
}

// ------------------------------------------------------------------------------------------------------------
